package isv

import (
    //"strconv"
    "dhfshop/models/isv"
	"dhfshop/controllers/base"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"wxsdk/miniapp/wxapp"
)

type IsvTesterController struct {
	base.BaseController
}

// 绑定微信用户为小程序体验者
func (this *IsvTesterController) TesterAdd() {
    v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
		WechatId         string  `form:"wechatid" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	code, err := wxapp.BindTester(custAccount.AccessToken,v.WechatId) 
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code   int     `json:"code"`
	    Msg    string  `json:"msg"`
	}{Code:code, Msg:""}
	this.Data["json"] = rsp
	this.ServeJSON()
}

// 解除绑定小程序的体验者
func (this *IsvTesterController) TesterDel() {
   v := struct {
	    AuthorizerAppid  string  `form:"authorizer_appid" valid:"Required"`
		WechatId         string  `form:"wechatid" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	code, err := wxapp.UnbindTester(custAccount.AccessToken,v.WechatId) 
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Code   int     `json:"code"`
	    Msg    string  `json:"msg"`
	}{Code:code, Msg:""}
	this.Data["json"] = rsp
	this.ServeJSON()
}


